import { TextAsset, Vec2 } from "cc";

export default class GameAsset {

	private static _round_layouts_: Map<string, RoundLayout> = new Map();
	/**
	 * 加载一个游戏布局
	 * @param file_name - 配置文件名
	 */
	public static async loadRoundLayout(file_name: string){
		if(GameAsset._round_layouts_.has(file_name)){
			return GameAsset._round_layouts_.get(file_name);
		}

		let config: ConfigLayout = await mtec.cc.loadResAsync('config/'+file_name, TextAsset).then(file=>mtec.parseJson(file.text.decodeAtoB));

		let layout: RoundLayout = config
		.filter(el=>el.element.reduce((acc, cur)=>acc+cur.coor.length, 0)>0)
		.sort((a, b)=>a.tier-b.tier)
		.map((el, i)=>{
			let coor = el.element.reduce((acc, cur)=>{
				acc.push(...cur.coor.map(c=>new Vec2(c.x, c.y+0.5)));
				return acc;
			}, [] as Vec2[]);

			return {tier: i+1, coor};
		});

		GameAsset._round_layouts_.set(file_name, layout);

		return layout;
	}

	private static _round_tiles_: Map<number, RoundTiles> = new Map();
	private static async getRoundTiles(round: number){
		if(GameAsset._round_tiles_.size==0){
			let list: Array<RoundTiles> = await mtec.cc.loadResAsync('config/round-tile', TextAsset).then(file=>mtec.parseJson(file.text.decodeAtoB));
			list.forEach(el=>GameAsset._round_tiles_.set(el.round+1, el));
		}

		return GameAsset._round_tiles_.get(round);
	}

	/**
	 * 加载关卡数据
	 * @param round - 关卡索引
	 */
	public static async loadRoundData(round: number){
		// 获取关卡的布局数据
		let layout = await GameAsset.loadRoundLayout('level_'+round);
		// 获取关卡的元素数据
		let round_tiles = await GameAsset.getRoundTiles(round);
		// 生成关卡的元素数据
		let id_list = mtec.array.create(20, i=>(i+1).toString().padStart(2, '0')).sort(()=>Math.random()-0.5);
		//id_list.unshift('00');
		id_list.unshift('13');

		let temp: Array<{tier: number; element: Map<string, Array<Vec2>>}> = [];

		round_tiles.tiers.forEach(conf=>{
			let bucket: [string, number][] = conf.tiles.map(({id, count})=>[id_list[id-1], count]);

			conf.tiers.forEach(tier=>{
				let item: typeof temp[number] = {tier, element: new Map()};
				temp.push(item);

				layout.find(t=>t.tier==tier).coor.forEach(c=>{
					let search = mtec.array.random(bucket);
					while(search[1]<=0){
						mtec.array.remove(bucket, search);
						search = mtec.array.random(bucket);
					}
					search[1]--;
					if(!item.element.has(search[0])){
						item.element.set(search[0], []);
					}
					item.element.get(search[0]).push(c);
				});
			});
		});

		return temp.map(({tier, element})=>{
			return {
				tier,
				element: Array.from(element).map(([id, coor])=>{
					return {id, coor};
				})
			}
		}).filter(el=>el.element.reduce((acc, cur)=>acc+cur.coor.length, 0)>0)
		.sort((a, b)=>a.tier-b.tier)
		.map((el, i)=>{
			el.tier = i+1;
			return el;
		});
	}
}

type RoundLayout = Array<{
	/** 层数索引 */
	tier: number;
	/** 包含当前层的坐标列表 */
	coor: Array<Vec2>
}>;

type ConfigLayout = Array<{
	tier: number;
	element: Array<{
		id: number;
		coor: Array<{x: number, y: number}>;
	}>;
}>;

type RoundTiles = {
	round: number;
	tiers: Array<{
		tiers: number[];
		tiles: Array<{id: number, count: number}>;
	}>;
};
